package com.liumuxian.user.web.controller;

import com.liumuxian.user.pojo.Admin;
import com.liumuxian.user.pojo.User;
import com.liumuxian.user.service.AdminService;
import com.liumuxian.user.service.UserService;
import com.liumuxian.user.wx.WeixinLoginParams;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    /**
     * 增加
     * @param admin
     */
    @PostMapping
    public Result add(@RequestBody Admin admin  ){
        adminService.saveAdmin(admin);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改
     * @param admin
     */
    @PutMapping("/{id}")
    public Result edit(@RequestBody Admin admin, @PathVariable String id ){
        admin.setId(id);
        adminService.updateAdmin(admin);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id ){
        adminService.deleteAdminById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result list(){
        return new Result(true,StatusCode.OK,"查询成功",adminService.findAdminList());
    }

    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @GetMapping("/{id}")
    public Result listById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",adminService.findAdminById(id));
    }

    /**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @PostMapping("/search")
    public Result list( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",adminService.findAdminList(searchMap));
    }

    /**
     * 分页+多条件查询
     * @param searchMap 查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @PostMapping("/search/{page}/{size}")
    public Result listPage(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<Admin> pageResponse = adminService.findAdminListPage(searchMap, page, size);
        return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Admin>(pageResponse.getTotalElements(), pageResponse.getContent()) );
    }

    //注入jwt工具类
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 管理员登录
     * @param loginMap
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map<String,String> loginMap){
        //调用业务层查询
        Admin admin =adminService.findAdminByAccountAndPassword(loginMap.get("account"),loginMap.get("password"));
        //判断
        if(null!=admin){
            //登录成功
            //签发token：
            String token = jwtUtil.createJWT(admin.getId(), admin.getAccount(), "admin");
            //使用临时map封装更多前端需要的数据
            Map<String,Object> map=new HashMap<>();
            map.put("token",token);
            //其他
            map.put("name",admin.getAccount());
            return new Result(true,StatusCode.OK,"登录成功",map);
        }else{
            return new Result(false,StatusCode.LOGINERROE,"用户名或密码错误");
        }
    }

}
